package com.lagou.edu.service.impl;

import com.lagou.edu.dao.LagouAuthCodeDao;
import com.lagou.edu.entity.LagouAuthCode;
import com.lagou.edu.service.CodeDubboService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author zhangjinshan
 * @version 1.0
 * @description
 * @date 2021/8/25 下午8:42
 */
@Service
@Slf4j
public class CodeDubboServiceImpl implements CodeDubboService {

    @Autowired
    private LagouAuthCodeDao lagouAuthCodeDao;

    @Override
    public Integer validate(String email, String code) {
        LagouAuthCode condition = new LagouAuthCode();
        condition.setCode(code);
        condition.setEmail(email);
        Example<LagouAuthCode> codeExample = Example.of(condition);
        LagouAuthCode lagouAuthCode = lagouAuthCodeDao.findOne(codeExample).get();
        if (ObjectUtils.isEmpty(lagouAuthCode)) {
            return Integer.valueOf(1);
        }
        Date expiretime = lagouAuthCode.getExpiretime();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(expiretime.toInstant(), ZoneId.systemDefault());
        if (localDateTime.isBefore(LocalDateTime.now())) {
            return Integer.valueOf(2);
        }
        return Integer.valueOf(0);
    }
}
